package com.zking.frame.lsx;

import java.awt.Color;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import com.zking.db.BaseDao;

// 增加面板
public class UpdateMsg extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private Font font;// 文字形态
	
	private JPanel pl_flow;
	private JLabel lb;// 头标前
	private JLabel lb_name;// 客户name
	private JLabel lb_pym;// 拼音码
	private JLabel lb_city;// 城市
	private JLabel lb_address;// 地址
	private JLabel lb_vip;// vip账号
	private JLabel lb_khh;// 开户行
	private JLabel lb_dh;// 电话
	private JLabel lb_yb;// 邮编
	private JLabel lb_cz;// 传真
	private JLabel lb_em;// E-mail
	private JLabel lb_lxr;// 联系人
	private JLabel lb_lxrsj;// 联系人手机
	
	private JTextField tf_name;
	private JTextField tf_pym;
	private JTextField tf_city;
	private JTextField tf_address;
	private JTextField tf_vip;
	private JTextField tf_khh;
	private JTextField tf_dh;
	private JTextField tf_yb;
	private JTextField tf_cz;
	private JTextField tf_em;
	private JTextField tf_lxr;
	private JTextField tf_lxrsj;
	
	private JPanel pl_bottom;// 下部面板
	private JButton bt_alter;// 修改
	private JButton bt_detele;// 删除
	private JButton bt_cancel;// 取消

	private JLabel lb_discount;// 折扣
	private List<Object> params;// 数据库参数
	private JTextField tf_discount;
	private String fixTime;// 修改时间
	public int things;// 1:修改2：删除3：取消
	
	public UpdateMsg(JDialog frame,boolean modal,List<String> lists,int id){
		super(frame, modal);
		font = new Font("宋体", Font.BOLD, 18);
		
		Container c = getContentPane();
		Color color0 = new Color(74, 101, 132);
		c.setBackground(color0);
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		
		// 中部面板
		pl_flow = new JPanel(new FlowLayout());// 流布局
		pl_flow.setPreferredSize(new Dimension(700, 400));
		Color color1 = new Color(222, 231, 239);
		pl_flow.setBackground(color1);
		gbc.gridx=0;
		gbc.gridy=0;
		c.add(pl_flow,gbc);
		
		JLabel label0 = new JLabel();
		label0.setPreferredSize(new Dimension(250, 40));
		pl_flow.add(label0);
		
		lb = new JLabel("客户资料修改");
		lb.setFont(font);
		lb.setPreferredSize(new Dimension(350, 40));
		pl_flow.add(lb);
		
		lb_name = new JLabel("名称：");
		lb_name.setFont(font);
		lb_name.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_name);
		
		tf_name = new JTextField();
		tf_name.setFont(font);
		Color color2 = new Color(255, 255, 225);
		tf_name.setPreferredSize(new Dimension(520, 25));
		tf_name.setBackground(color2);
		tf_name.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_name);
		
		lb_pym = new JLabel("拼音码：");
		lb_pym.setFont(font);
		lb_pym.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_pym);
		
		tf_pym = new JTextField();
		tf_pym.setFont(font);
		tf_pym.setPreferredSize(new Dimension(150, 25));
		tf_pym.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_pym);
		
		JLabel label1 = new JLabel();// 填充板
		label1.setPreferredSize(new Dimension(125, 40));
		pl_flow.add(label1);
		
		lb_city = new JLabel("城市：");
		lb_city.setFont(font);
		lb_city.setPreferredSize(new Dimension(80, 40));
		pl_flow.add(lb_city);
		
		tf_city = new JTextField();
		tf_city.setFont(font);
		tf_city.setPreferredSize(new Dimension(150, 25));
		tf_city.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_city);
		
		lb_address = new JLabel("地址：");
		lb_address.setFont(font);
		lb_address.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_address);
		
		tf_address = new JTextField();
		tf_address.setFont(font);
		tf_address.setPreferredSize(new Dimension(150, 25));
		tf_address.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_address);
		
		JLabel label01 = new JLabel();// 填充板
		label01.setPreferredSize(new Dimension(115, 40));
		pl_flow.add(label01);
		
		lb_discount = new JLabel("折 扣：");
		lb_discount.setFont(font);
		lb_discount.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_discount);
		
		tf_discount = new JTextField("0.85");
		tf_discount.setFont(font);
		tf_discount.setPreferredSize(new Dimension(150, 25));
		tf_discount.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		tf_discount.setEditable(false);
		pl_flow.add(tf_discount);
		
		JLabel label2 = new JLabel();// 填充板
		label2.setPreferredSize(new Dimension(615, 3));
		label2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		pl_flow.add(label2);

		// 由于每一行的添加极其相似，所以写成 setHang方法
		// 1. 参数：（这一行的第1个标签，文本域1，第2个标签，文本域2）
		lb_vip=new JLabel("vip：");
		tf_vip=new JTextField();
		lb_khh=new JLabel("开户行：");
		tf_khh=new JTextField();
		setHang(lb_vip, tf_vip,  lb_khh, tf_khh);
		
		lb_dh=new JLabel("电话：");
		tf_dh=new JTextField();
		lb_yb=new JLabel("邮编：");
		tf_yb=new JTextField();
		setHang(lb_dh, tf_dh, lb_yb, tf_yb );
		
		lb_cz=new JLabel( "传真：");
		tf_cz=new JTextField();
		lb_em=new JLabel("E-mail：");
		tf_em=new JTextField();
		setHang(lb_cz, tf_cz, lb_em, tf_em);
		
		lb_lxr=new JLabel("联系人：");
		tf_lxr=new JTextField();
		lb_lxrsj=new JLabel("手机：");
		tf_lxrsj=new JTextField();
		setHang(lb_lxr, tf_lxr, lb_lxrsj, tf_lxrsj);
		// 下部面板
		pl_bottom = new JPanel(new GridBagLayout());
		pl_bottom.setOpaque(false);// 透明
		pl_bottom.setPreferredSize(new Dimension(700, 90));
		gbc.gridx=0;
		gbc.gridy=1;
		c.add(pl_bottom,gbc);
		
		bt_alter = new JButton("修改");
		bt_alter.setFont(new Font("宋体", Font.BOLD, 14));
		bt_alter.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		bt_alter.setOpaque(false);
		bt_alter.setPreferredSize(new Dimension(100, 35));
		pl_bottom.add(bt_alter);
		bt_alter.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lists.clear();// 清除
				Tools t = new Tools();
				boolean flag = t.isMobileNO(tf_dh.getText());
				// 判断是否存在姓名和电话号码
				if ("".equals(tf_name.getText())||tf_name.getText().length()==0) {
					JOptionPane.showMessageDialog(UpdateMsg.this, "名称不能为空");
				}else if ("".equals(tf_dh.getText())||tf_dh.getText().length()==0) {
					JOptionPane.showMessageDialog(UpdateMsg.this, "电话不能为空");
				}else if(flag==false){
					JOptionPane.showMessageDialog(UpdateMsg.this, "电话号码有误");
				}else{
					// 修改后的数据
					lists.add(tf_name.getText());
					lists.add(tf_pym.getText());
					lists.add(tf_city.getText());
					lists.add(tf_address.getText());
					lists.add(tf_khh.getText());
					
					lists.add(tf_dh.getText());
					lists.add(tf_yb.getText());
					lists.add(tf_cz.getText());
					lists.add(tf_em.getText());
					lists.add(tf_lxr.getText());
					
					lists.add(tf_lxrsj.getText());
					lists.add(tf_discount.getText());
					lists.add(tf_vip.getText());
					// 修改时间
					fixTime=t.getTime();
					lists.add(fixTime);
					// 要传的参数
					params = new ArrayList<Object>();
					for (int i = 0; i < lists.size(); i++) {
						// 获取数据
						String data=lists.get(i);
						// 添加参数
						if(data==null||"".equals(data)||data.length()==0){
							params.add(null);
						}else{
							params.add(data);
						}
					}
					BaseDao base=new BaseDao();
					String sql="update client set "
							+ "name=?,pinYinCode=?,city=?,address=?,bankOfDeposit=?,"
							+ "tel=?,postalCode=?,fax=?,e_mail=?,contactPerson=?,"
							+ "contactTel=?,discount=?,isVIP=?,fixTime=? where id="+id;
					try {
						base.update(sql,params.toArray());
					} catch (SQLException e1) {
						e1.printStackTrace();
					}finally{
						try {
							base.closeConnection();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
					}
					things=1;
					// 退出现在的面板
					dispose();// 退出
				}
			}
		});
		
				//添加修改前数据
				tf_name.setText(lists.get(0));
				tf_pym.setText(lists.get(1));
				tf_city.setText(lists.get(2));
				tf_address.setText(lists.get(3));
				tf_discount.setText(lists.get(4));
				tf_vip.setText(lists.get(5));
				tf_khh.setText(lists.get(6));
				tf_dh.setText(lists.get(7));
				tf_yb.setText(lists.get(8));
				tf_cz.setText(lists.get(9));
				tf_em.setText(lists.get(10));
				tf_lxr.setText(lists.get(11));
				tf_lxrsj.setText(lists.get(12));
				lists.clear();
				
		JLabel label4 = new JLabel();// 填充板
		label4.setPreferredSize(new Dimension(60, 30));
		pl_bottom.add(label4);
		
		bt_detele = new JButton("删除");
		bt_detele.setFont(new Font("宋体", Font.BOLD, 14));
		bt_detele.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		bt_detele.setOpaque(false);
		bt_detele.setPreferredSize(new Dimension(100, 35));
		pl_bottom.add(bt_detele);
		bt_detele.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				int select = JOptionPane.showConfirmDialog(null, "确认删除吗，删除后数据将无法恢复？", "提示", JOptionPane.YES_NO_OPTION);
				if (select != JOptionPane.YES_OPTION) {
						return;
				}
				BaseDao base =new BaseDao();
				// 删除语句
				String sql1 ="delete from arap WHERE customerID=?";//删除应付应收
				String sql2 ="delete from  wholesalecontent  WHERE wholeSaleId=?";//删除批发内容
				String sql3 ="SELECT id FROM wholesale WHERE customerId=?";//批发单id
				String sql4 ="delete from client where id=?";//删除客户
				 try {
					base.update(sql1,id);//删除应付应收
					int wholesales_id=base.getCount(sql3,id);//批发ID
					base.update(sql2,wholesales_id);//删除批发内容
					base.update("delete FROM wholesale WHERE customerId=?",id);//删除批发
				    base.update(sql4,id);
				 } catch (Exception e2) {
					e2.printStackTrace();
				}
				
				lists.removeAll(lists);// 移除
				things=2;
				dispose();// 退出
			}
		});
		
		JLabel label5 = new JLabel();// 填充板
		label5.setPreferredSize(new Dimension(60, 30));
		pl_bottom.add(label5);
		
		bt_cancel = new JButton("取消");
		bt_cancel.setFont(new Font("宋体", Font.BOLD, 14));
		bt_cancel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		bt_cancel.setOpaque(false);
		bt_cancel.setPreferredSize(new Dimension(100, 35));
		pl_bottom.add(bt_cancel);
		bt_cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				things=3;
				dispose();// 直接退出
			}
		});
		
		setTitle("资料修改");
		setSize(800, 600);
		setResizable(false);// 不可增加大小
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	public void setHang(JLabel lb1,JTextField tf1,JLabel lb2,JTextField tf2){
		lb1.setFont(font);
		lb1.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb1);
		
		tf1.setFont(font);
		tf1.setPreferredSize(new Dimension(150, 25));
		tf1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf1);
		
		JLabel label = new JLabel();// 填充板
		label.setPreferredSize(new Dimension(115, 40));
		pl_flow.add(label);
		
		lb2.setFont(font);
		lb2.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb2);
		
		tf2.setFont(font);
		tf2.setPreferredSize(new Dimension(150, 25));
		tf2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf2);
	}
}

